package dao;

import Bean.*;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserCheck {
    public User selectUserByUsernameAndPasswod(String username,String password) throws SQLException {
        String sql="select * from u_login where username=? and password =?";
        QueryRunner qr=new QueryRunner(JDBCUtil.getDatasource());
       User user=qr.query(sql, new BeanHandler<>(User.class),username,password);
        return user;

    }
    public  Boolean selecttUser(String username,String password) throws SQLException {
        List<User> list=new ArrayList<>();
        String sql="select  * from u_login where username=? and password=?";
        QueryRunner qr=new QueryRunner(JDBCUtil.getDatasource());
        User user=qr.query(sql,new BeanHandler<>(User.class),username,password);
        //用户名不为空则不可以插入  为空则可以进行插入
        if (user==null) {
            return true;
        }else {
       return false;}
    }
    public  Boolean insertUser(String username,String password) throws SQLException {
        String sql="insert into u_login(username,password) values(?,?)";
        QueryRunner qr=new QueryRunner(JDBCUtil.getDatasource());
         int i=qr.update(sql,username,password);
         //i返回0插入失败否则插入成功
         if (i==0){
             return false;
         }else {
             return true;
         }
    }
}
